#include <iostream>
#include <memory>
#include <unistd.h>
#include <pthread.h>
#include "ring_queue.hpp"

using data = int;

void* producer(void* _rq)
{
    ring_queue_module::ring_queue<data>& rq = *(ring_queue_module::ring_queue<data>*)_rq;
    int val = 0;
    while (true)
    {
        //使得生产者总是慢于消费者，这样队列一直都只会有一个元素
        // sleep(2);
        rq.push(val);
        val++;
    }
}


void* consumer(void* _rq)
{
    ring_queue_module::ring_queue<data>& rq = *(ring_queue_module::ring_queue<data>*)_rq;
    data val = 0;
    while (true)
    {
        //使得生产者总是慢于消费者，这样队列一直都满
        sleep(1);
        val = rq.pop();
    }
}


void test()
{
    pthread_t tid_consumer1 = 0, tid_producer1 = 0;
    pthread_t tid_consumer2 = 1, tid_producer2 = 0;
    pthread_t tid_consumer3 = 2, tid_producer3 = 0;
    std::shared_ptr<ring_queue_module::ring_queue<data>> bq(std::make_shared<ring_queue_module::ring_queue<data>>(5));
    pthread_create(&tid_producer1, nullptr, producer, &(*bq));
    // pthread_create(&tid_producer2, nullptr, producer, &(*bq));
    // pthread_create(&tid_producer3, nullptr, producer, &(*bq));
    pthread_create(&tid_consumer1, nullptr, consumer, &(*bq));
    // pthread_create(&tid_consumer2, nullptr, consumer, &(*bq));
    // pthread_create(&tid_consumer3, nullptr, consumer, &(*bq));
    pthread_join(tid_producer1, nullptr);
    // pthread_join(tid_producer2, nullptr);
    // pthread_join(tid_producer3, nullptr);
    pthread_join(tid_consumer1, nullptr);
    // pthread_join(tid_consumer2, nullptr);
    // pthread_join(tid_consumer3, nullptr);
    
}


int main()
{
    test();
    return 0;
}